package fei.yuzhi.com.loveflowers.fragment.mefragment.meSet;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import org.greenrobot.eventbus.EventBus;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.Bmob;
import cn.bmob.v3.update.BmobUpdateAgent;
import fei.yuzhi.com.loveflowers.R;
import fei.yuzhi.com.loveflowers.fragment.mefragment.MeFragment;
import fei.yuzhi.com.loveflowers.fragment.mefragment.meSet.aboutUs.AboutUsActivity;

public class SetActivity extends AppCompatActivity {

    @BindView(R.id.iv_meset_back)
    ImageView ivMesetBack;
    @BindView(R.id.linl_meset_about)
    RelativeLayout linlMesetAbout;
    @BindView(R.id.linl_meset_new)
    RelativeLayout linlMesetNew;
    @BindView(R.id.linl_meset_clean)
    RelativeLayout linlMesetClean;
    @BindView(R.id.tv_meset_new_banben)
    TextView tvMesetNewBanben;
    @BindView(R.id.btn_meset_back_login)
    TextView btnMesetBackLogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set);
        ButterKnife.bind(this);
        Bmob.initialize(this, "7c6d1a4bf3aafd9f814667d6ad3cf6bb");
        int code = getPackageInfo(this).versionCode;
        tvMesetNewBanben.setText(code + "版本");
    }

    @OnClick({R.id.btn_meset_back_login,R.id.iv_meset_back, R.id.linl_meset_about, R.id.linl_meset_new, R.id.linl_meset_clean})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_meset_back:
                finish();
                break;
            case R.id.linl_meset_about:
                Intent intent = new Intent(SetActivity.this, AboutUsActivity.class);
                startActivity(intent);
                break;
            case R.id.linl_meset_new:
                //BmobUpdateAgent.initAppVersion();
                BmobUpdateAgent.forceUpdate(SetActivity.this);
                break;
            case R.id.linl_meset_clean:
                deleteFilesByDirectory(this.getCacheDir());
                Toast.makeText(SetActivity.this, "清除完毕", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_meset_back_login:
                EventBus.getDefault().post(new Boolean(false));
                finish();
                break;
        }
    }

    /**
     * 删除方法 这里只会删除某个文件夹下的文件，如果传入的directory是个文件，将不做处理 * * @param directory
     */
    private static void deleteFilesByDirectory(File directory) {
        if (directory != null && directory.exists() && directory.isDirectory()) {
            for (File item : directory.listFiles()) {
                item.delete();
            }
        }
    }

    private static PackageInfo getPackageInfo(Context context) {
        PackageInfo pi = null;

        try {
            PackageManager pm = context.getPackageManager();
            pi = pm.getPackageInfo(context.getPackageName(),
                    PackageManager.GET_CONFIGURATIONS);
            return pi;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pi;
    }


}
